package com.example.demo.service.impl;


import com.example.demo.entity.BookinfoEntity;
import com.example.demo.entity.BookstateEntity;
import com.example.demo.repository.BookinfoRepository;
import com.example.demo.repository.BookstateRepository;
import com.example.demo.service.BookinfoService;
import com.example.demo.util.BookConverter;
import com.example.demo.vo.BookinfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: wuhen
 * Date: 2018/10/9
 * Time: 20:48
 */
@Service
public class BookinfoServiceImpl implements BookinfoService {
    @Autowired
    private BookinfoRepository bookinfoRepository;
    @Autowired
    private BookstateRepository bookstateRepository;
    BookConverter bookConverter=new BookConverter();
    @Override
    public BookinfoVo findbookOne(Integer bookId) {

        BookinfoEntity bookinfoEntity= bookinfoRepository.findById(bookId).get();
        BookstateEntity bookstateEntity = bookstateRepository.findById(bookinfoEntity.getBookState()).get();
        BookinfoVo bookinfoVo;
        bookinfoVo = bookConverter.convert(bookinfoEntity, BookinfoVo.class);
        bookinfoVo.setBookStateinfo(bookstateEntity.getBookStateinfo());
        return bookinfoVo;
    }

    @Override
    public List<BookinfoVo> findbookAll() {
        List<BookinfoEntity> bookinfoEntitys=bookinfoRepository.findAll();
        List<BookinfoVo> bookinfoVos=new ArrayList<>();

        for (BookinfoEntity bookinfoEntity:bookinfoEntitys)
        {
            BookinfoVo bookinfoVo;
            BookstateEntity bookstateEntity= bookstateRepository.findById(bookinfoEntity.getBookState()).get();
            bookinfoVo=bookConverter.convert(bookinfoEntity,BookinfoVo.class);
            bookinfoVo.setBookStateinfo(bookstateEntity.getBookStateinfo());
            bookinfoVos.add(bookinfoVo);
        }

       return bookinfoVos;
    }
}
